package managedbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import session.stateless.EmployeeSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import entity.Employee;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Calendar;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.LogSessionBeanLocal;
import session.stateless.NoticeSessionBeanLocal;

@ManagedBean(name = "noticeManagerBean")
@RequestScoped
public class NoticeManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @EJB
    private NoticeSessionBeanLocal noticeSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    
    private String loadNotice;
    private String curDate;
    private String loadAddNotice;
    private String description;
    private String title;
    private Timestamp noticeDate;
    private String department;
    private Boolean checkSales;
    private Boolean checkMarketing;
    private Boolean checkSupply;
    private Boolean checkProduction;
    private Boolean checkTechnology;
    private Boolean checkFinance;

    public NoticeManagerBean() {
    }
// <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the employeeSessionBeanLocal
     */
    public EmployeeSessionBeanLocal getEmployeeSessionBeanLocal() {
        return employeeSessionBeanLocal;
    }

    /**
     * @param employeeSessionBeanLocal the employeeSessionBeanLocal to set
     */
    public void setEmployeeSessionBeanLocal(EmployeeSessionBeanLocal employeeSessionBeanLocal) {
        this.employeeSessionBeanLocal = employeeSessionBeanLocal;
    }

    /**
     * @return the noticeSessionBeanLocal
     */
    public NoticeSessionBeanLocal getNoticeSessionBeanLocal() {
        return noticeSessionBeanLocal;
    }

    /**
     * @param noticeSessionBeanLocal the noticeSessionBeanLocal to set
     */
    public void setNoticeSessionBeanLocal(NoticeSessionBeanLocal noticeSessionBeanLocal) {
        this.noticeSessionBeanLocal = noticeSessionBeanLocal;
    }

    /**
     * @return the curDate
     */
    public String getCurDate() {
        return curDate;
    }

    /**
     * @param curDate the curDate to set
     */
    public void setCurDate(String curDate) {
        this.curDate = curDate;
    }

    /**
     * @return the loadNotice
     */
    public String getLoadNotice() {
        return loadNotice;
    }

    /**
     * @param loadNotice the loadNotice to set
     */
    public void setLoadNotice(String loadNotice) {
        this.loadNotice = loadNotice;
    }

    /**
     * @return the loadAddNotice
     */
    public String getLoadAddNotice() {
        return loadAddNotice;
    }

    /**
     * @param loadAddNotice the loadAddNotice to set
     */
    public void setLoadAddNotice(String loadAddNotice) {
        this.loadAddNotice = loadAddNotice;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the noticeDate
     */
    public Timestamp getNoticeDate() {
        return noticeDate;
    }

    /**
     * @param noticeDate the noticeDate to set
     */
    public void setNoticeDate(Timestamp noticeDate) {
        this.noticeDate = noticeDate;
    }

    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }

    /**
     * @param department the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }

    /**
     * @return the checkSales
     */
    public Boolean getCheckSales() {
        return checkSales;
    }

    /**
     * @param checkSales the checkSales to set
     */
    public void setCheckSales(Boolean checkSales) {
        this.checkSales = checkSales;
    }

    /**
     * @return the checkMarketing
     */
    public Boolean getCheckMarketing() {
        return checkMarketing;
    }

    /**
     * @param checkMarketing the checkMarketing to set
     */
    public void setCheckMarketing(Boolean checkMarketing) {
        this.checkMarketing = checkMarketing;
    }

    /**
     * @return the checkSupply
     */
    public Boolean getCheckSupply() {
        return checkSupply;
    }

    /**
     * @param checkSupply the checkSupply to set
     */
    public void setCheckSupply(Boolean checkSupply) {
        this.checkSupply = checkSupply;
    }

    /**
     * @return the checkProduction
     */
    public Boolean getCheckProduction() {
        return checkProduction;
    }

    /**
     * @param checkProduction the checkProduction to set
     */
    public void setCheckProduction(Boolean checkProduction) {
        this.checkProduction = checkProduction;
    }

    /**
     * @return the checkTechnology
     */
    public Boolean getCheckTechnology() {
        return checkTechnology;
    }

    /**
     * @param checkTechnology the checkTechnology to set
     */
    public void setCheckTechnology(Boolean checkTechnology) {
        this.checkTechnology = checkTechnology;
    }

    /**
     * @return the checkFinance
     */
    public Boolean getCheckFinance() {
        return checkFinance;
    }

        /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }
    
    
    /**
     * @param checkFinance the checkFinance to set
     */
    public void setCheckFinance(Boolean checkFinance) {
        this.checkFinance = checkFinance;
    }

    // </editor-fold>
    public void addNotice(ActionEvent event) {
        Calendar cal = Calendar.getInstance();
        Timestamp ts = new Timestamp(cal.getTime().getTime());
        Employee employee = getEmployeeSessionBeanLocal().getEmployee(getUserSessionManagerBean().getUserId());

        setDepartment(employee.getDepartment());

        if (checkSales) {
            setDepartment(getDepartment() + ";Sales");
        }
        if (checkMarketing) {
            setDepartment(getDepartment() + ";Marketing");
        }
        if (checkSupply) {
            setDepartment(getDepartment() + ";Supply chain");
        }
        if (checkProduction) {
            setDepartment(getDepartment() + ";Production and Operation");
        }
        if (checkTechnology) {
            setDepartment(getDepartment() + ";Technology");
        }
        if (checkFinance) {
            setDepartment(getDepartment() + ";Finance");
        }

        getNoticeSessionBeanLocal().addNotice(getDepartment(), getDescription(), ts, getUserSessionManagerBean().getUserId(), title);
        logSessionBeanLocal.addLog("Add notice", "Notice created by: " + getUserSessionManagerBean().getUserId() + " on " + ts,"addNotice.xhtml", userSessionManagerBean.getUserId());
        try {

  ArrayList<String> al = new ArrayList<String>();
            al.add("Notice added");
            al.add("You have successfully added a notice. This notice will be boardcast to the selected department.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("index.xhtml");
            userSessionManagerBean.setSelectedItem(al);   


            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort()
                    + "/MerlionERP-war/success.xhtml");
        } catch (Exception ex) {}
        

    }


}
